package com.yupi.yuoj.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.yupi.yuoj.judge.codesandbox.model.JudgeInfo;
import com.yupi.yuoj.model.dto.question.JudgeCase;
import com.yupi.yuoj.model.dto.question.JudgeConfig;
import com.yupi.yuoj.model.entity.Question;
import com.yupi.yuoj.model.enums.JudgeInfoMessageEnum;

import java.util.List;

public class CppLanguageJudgeStrategy implements JudgeStrategy{
    final long CPP_TIME_OFFSET = 1000L;
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {

        JudgeInfoMessageEnum judgeInfoMessage = JudgeInfoMessageEnum.ACCEPTED;


        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        List<String> inputList = judgeContext.getInputList();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        List<String> outputList = judgeContext.getOutputList();
        Question question = judgeContext.getQuestion();

        JudgeInfo judgeInfoResponse = new JudgeInfo();
        String judgeConfigStr = question.getJudgeConfig();
        //!!!
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr , JudgeConfig.class);
        Long memory = judgeInfo.getMemory();
        Long time = judgeInfo.getTime();


        judgeInfoResponse.setTime(time);
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setMessage(judgeInfoMessage.getValue());




        //判断输入和输出的样例是否相等
        if(outputList.size() != inputList.size()){
            judgeInfoMessage = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessage.getValue());
            return judgeInfoResponse;
        }

        //判断每一项的执行答案和标准答案是否相等
        for(int i = 0 ; i < judgeCaseList.size() ; i++){
            JudgeCase judgeCase = judgeCaseList.get(i);
            if(!judgeCase.equals(outputList.get(i))){
                judgeInfoMessage = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessage.getValue());
                return judgeInfoResponse;
            }
        }

        //判断题目限制



        if(memory > judgeConfig.getMemoryLimit()){
            judgeInfoMessage = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessage.getValue());
            return judgeInfoResponse;
        }

        if(time - CPP_TIME_OFFSET > judgeConfig.getTimeLimit()){
            judgeInfoMessage = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessage.getValue());
            return judgeInfoResponse;
        }





        return judgeInfoResponse;

    }
}